﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

namespace WindowsFormsApp1
{
    
    public partial class Form1 : Form
    {
        private bool start;//游戏是否开始
        private bool ChessCheck = true;//白子黑子回合
        private const int size = 15;//棋盘大小

        int[,] CheckBoard = new int[size, size];//期盼点位数组


        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeGame();
            this.Width = MainSize.FormWidth;
            this.Height = MainSize.FormHeight;
            this.Location = new Point(400, 75);
        }
        //初始化游戏
        private void InitializeGame()
        {
            for (int i=0;i<size  ;i++ )
            {
                for(int j=0;j<size ;j++ )
                {
                    CheckBoard[i, j] = 0;

                }
            }
            start = false;
            label1.Text = "游戏未开始";
            button1.Visible = true;
            button2.Visible = false;
            Graphics g = panel1.CreateGraphics();
            ChessBoard.DrawCB(g);
            Chess.ReDrawC(panel1, CheckBoard);

        }

      
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            ChessBoard.DrawCB(g);
            Chess.ReDrawC(panel1, CheckBoard);
        }
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            panel2.Size = new Size(MainSize.FormWidth-MainSize.CBoardWidth,MainSize.FormHeight);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            start = true;
            ChessCheck = true;
            label1.Text = "黑子回合";
            button1.Visible = false;
            button2.Visible = true;
            panel2.Invalidate();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("是否确认要重新开始？","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)==DialogResult.OK)
            {
                InitializeGame();
                button1_Click(sender, e);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            Process process = new Process();
            process.StartInfo.FileName = @"..\..\..\..\..\..\..\..\总体框架\总体框架\总体框架\bin\Debug\总体框架.exe";
            process.Start();
            this.Dispose();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if(start)
            {
                int Judgment = 0;


                int PlacementX = e.X / MainSize.CBoardGap;
                int PlacementY = e.Y / MainSize.CBoardGap;
                try
                {
                    if (CheckBoard[PlacementX,PlacementY]!=0)
                    {
                        return;
                    }
                    if (ChessCheck)
                    {
                        CheckBoard[PlacementX, PlacementY] = 1;
                        Judgment = 1;
                        label1.Text = "白子回合";
                    }
                    else
                    {
                        CheckBoard[PlacementX, PlacementY] = 2;
                        Judgment = 2;
                        label1.Text = "黑子回合";
                    }
                    Chess.DrawC(panel1,ChessCheck,PlacementX,PlacementY);


                    //胜利判断
                    if(WinJudgment.CheckJudgment(CheckBoard,Judgment))
                    {
                        if(Judgment==1)
                        {
                            MessageBox.Show("黑子胜利！");
                        }
                        else
                        {
                            MessageBox.Show("白子胜利");
                        }  
                    }
                    ChessCheck = !ChessCheck;
                }
                catch(Exception)
                {

                }
            }
            else
            {
                MessageBox.Show("请先开始游戏！", "提示!");
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = @"..\..\..\..\..\..\..\..\总体框架\总体框架\总体框架\bin\Debug\总体框架.exe";
            process.Start();
        }
    }
}
